home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacPerl 4.1.3 / MacPerl / MPWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  28.5 KB  |  833 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    MacPerl            -    Real Perl Application
  3. File        :    MPWindow.c        -
  4. Author    :    Matthias Neeracher
  5.  
  6. A lot of this code is borrowed from 7Edit written by
  7. Apple Developer Support UK
  8.  
  9. Language    :    MPW C
  10.  
  11. $Log: MPWindow.c,v $
  12. Revision 1.2  1994/05/04  02:52:40  neeri
  13. Inline Input.
  14.  
  15. Revision 1.1  1994/02/27  23:02:22  neeri
  16. Initial revision
  17.  
  18. Revision 0.4  1993/08/17  00:00:00  neeri
  19. A little more defensiveness
  20.  
  21. Revision 0.3  1993/08/06  00:00:00  neeri
  22. Draw pretty icons
  23.  
  24. Revision 0.2  1993/05/30  00:00:00  neeri
  25. Support Console Windows
  26.  
  27. Revision 0.1  1993/05/29  00:00:00  neeri
  28. Compiles correctly
  29.  
  30. *********************************************************************/
  31.  
  32. #include <Resources.h>
  33. #include <Scrap.h>
  34. #include <Packages.h>
  35. #include <PLStringFuncs.h>
  36. #include <Icons.h>
  37. #include "MPWindow.h"
  38. #include "MPConsole.h"
  39. #include "MPEditions.h"
  40.  
  41. #define    kControlInvisible    0
  42. #define    kControlVisible      0xFF
  43. #define    kScrollbarWidth        16
  44. #define    kScrollbarAdjust        (kScrollbarWidth - 1)
  45. #define    kScrollTweek           2
  46. #define    kTextOffset                5
  47. #define    kButtonScroll          10
  48.  
  49. #define    kMaxPages              1000 /* Assumes pages > 32 pixels high */
  50.  
  51. #define    kHOffset                        20   /* Stagger window offsets */
  52. #define    kVOffset                        20
  53.  
  54. #define    kTBarHeight                20
  55. #define    kMBarHeight                20
  56.  
  57. typedef short PageEndsArray[kMaxPages];
  58.  
  59. #pragma segment Window
  60.  
  61. pascal DPtr DPtrFromWindowPtr(WindowPtr w)
  62. {
  63.     if (w)
  64.         return((DPtr)GetWRefCon(w));
  65.     else
  66.         return(nil);
  67. } /* DPtrFromWindowPtr */
  68.  
  69. #pragma segment main
  70.  
  71. /*
  72.   Scroll the TERec around to match up to the potentially updated scrollbar
  73.   values. This is really useful when the window resizes such that the
  74.   scrollbars become inactive and the TERec had been previously scrolled.
  75. */
  76. pascal void AdjustTE(DPtr theDoc)
  77. {
  78.     short    h;
  79.     short    v;
  80.     TEHandle myText;
  81.  
  82.     myText = theDoc->theText;
  83.     h =
  84.         ((*myText)->viewRect.left - (*myText)->destRect.left) -
  85.         GetCtlValue(theDoc->hScrollBar) + kTextOffset;
  86.  
  87.     v =
  88.         ((*myText)->viewRect.top - (*myText)->destRect.top) -
  89.         GetCtlValue(theDoc->vScrollBar) + kTextOffset;
  90.  
  91.     if (h || v) {
  92.         TEScroll(h, v, theDoc->theText);
  93.         DrawPageExtras(theDoc);
  94.     }
  95. }  /* AdjustTE */
  96.  
  97.  
  98. /*Calculate the new control maximum value and current value, whether it is the horizontal or
  99. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  100. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  101. width to the width of the viewRect. The current values are set by comparing the offset between
  102. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  103. calling ShowControl.*/
  104.  
  105. /*TEStyleSample-vertical max originally used line by line calculations-lineheight was a
  106. constant value so it was easy to figure out what the range should be and pin the value
  107. within range. Now we need to use max and min values in pixels rather than in nlines*/
  108.  
  109. #pragma segment main
  110.  
  111. pascal void AdjustHV(
  112.     Boolean        isVert,
  113.     ControlHandle  control,
  114.     DPtr           theDoc,
  115.     Boolean        canRedraw)
  116. {
  117.     TEHandle    docTE;
  118.     short       value;
  119.     short           max;
  120.     short           oldValue;
  121.     short           oldMax;
  122.     Rect             sizeRect;
  123.  
  124.     sizeRect = theDoc->pageSize;
  125.     docTE    = theDoc->theText;
  126.  
  127.     oldValue = GetCtlValue(control);
  128.     oldMax   = GetCtlMax(control);
  129.     if (isVert)
  130.         max = (*docTE)->nLines*(*docTE)->lineHeight - ((*docTE)->viewRect.bottom - (*docTE)->viewRect.top);
  131.     else
  132.         max = sizeRect.right - ((*docTE)->viewRect.right - (*docTE)->viewRect.left);
  133.  
  134.     max += kTextOffset + kTextOffset; /* Allow over scroll by kTextOffset */
  135.  
  136.     if (max < 0)
  137.         max = 0; /* check for negative values */
  138.  
  139.     SetCtlMax(control, max);
  140.  
  141.     if (isVert)
  142.         value = (*docTE)->viewRect.top - (*docTE)->destRect.top;
  143.     else
  144.         value = (*docTE)->viewRect.left - (*docTE)->destRect.left;
  145.  
  146.     value += kTextOffset;
  147.  
  148.     if (value < 0) {
  149.         TEScroll(isVert ? 0 : value, isVert ? value : 0, docTE);
  150.         DrawPageExtras(theDoc);
  151.  
  152.         value = 0;
  153.     } else if (value > max) {
  154.         TEScroll(isVert ? 0 : value-max, isVert ? value-max : 0, docTE);
  155.         DrawPageExtras(theDoc);
  156.         
  157.         value = max; /* pin the value to within range */
  158.     }
  159.     SetCtlValue(control, value);
  160.     if (canRedraw && ((max != oldMax) || (value != oldValue)))
  161.         ShowControl(control); /* check to see if the control can be re-drawn */
  162. } /* AdjustHV */
  163.  
  164. #pragma segment Main
  165.  
  166. pascal void AdjustScrollValues(DPtr theDoc, Boolean canRedraw)
  167. {
  168.     AdjustHV(true,  theDoc->vScrollBar, theDoc, canRedraw);
  169.     AdjustHV(false, theDoc->hScrollBar, theDoc, canRedraw);
  170. }        /* AdjustScrollValues */
  171.  
  172. pascal void GetTERect(WindowPtr window, Rect  *teRect)
  173. {
  174.      *teRect = window->portRect;
  175.      (*teRect).bottom -= kScrollbarAdjust; /* and for the scrollbars */
  176.      (*teRect).right  -= kScrollbarAdjust;
  177. }         /* GetTERect */
  178.  
  179. /* Re-calculate the position and size of the viewRect and the scrollbars.
  180.   kScrollTweek compensates for off-by-one requirements of the scrollbars
  181.   to have borders coincide with the growbox. */
  182.  
  183. pascal void AdjustScrollSizes(DPtr theDoc)
  184. {
  185.     Rect    teRect;
  186.     Rect    myPortRect;
  187.  
  188.     GetTERect(theDoc->theWindow, &teRect); /*start with teRect*/
  189.     myPortRect = theDoc->theWindow->portRect;
  190.  
  191.     (*(theDoc->theText))->viewRect = teRect;
  192.  
  193.     MoveControl(theDoc->vScrollBar, myPortRect.right - kScrollbarAdjust, -1);
  194.     SizeControl(
  195.         theDoc->vScrollBar,
  196.         kScrollbarWidth,
  197.         (myPortRect.bottom - myPortRect.top) - (kScrollbarAdjust - kScrollTweek));
  198.  
  199.     MoveControl(theDoc->hScrollBar, 31, myPortRect.bottom - kScrollbarAdjust);
  200.     SizeControl(
  201.         theDoc->hScrollBar,
  202.         (myPortRect.right - myPortRect.left) - (kScrollbarAdjust - kScrollTweek + 32),
  203.         kScrollbarWidth);
  204. }        /* AdjustScrollSizes */
  205.  
  206. #pragma segment Window
  207.  
  208. /* Turn off the controls by jamming a zero into their contrlVis fields
  209.   (HideControl erases them and we don't want that). If the controls are to
  210.   be resized as well, call the procedure to do that, then call the procedure
  211.   to adjust the maximum and current values. Finally reset the controls
  212.   to be visible if not in background. */
  213.  
  214. pascal void AdjustScrollbars(DPtr theDoc, Boolean  needsResize)
  215. {
  216.     Boolean    background = gInBackground || theDoc->theWindow != gActiveWindow;
  217.     
  218.     (*(theDoc->vScrollBar))->contrlVis = kControlInvisible; /* turn them off */
  219.     (*(theDoc->hScrollBar))->contrlVis = kControlInvisible;
  220.  
  221.     if (needsResize) /* move and size if needed */
  222.         AdjustScrollSizes(theDoc);
  223.  
  224.     AdjustScrollValues(theDoc, !needsResize && !background); /* fool with max and current value */
  225.  
  226.      /* Now, restore visibility in case we never had to ShowControl during adjustment */
  227.  
  228.     if (!background) {
  229.         (*(theDoc->vScrollBar))->contrlVis = kControlVisible; /* turn them on */
  230.         (*(theDoc->hScrollBar))->contrlVis = kControlVisible;
  231.     } else { /* make sure they stay invisible */
  232.         if ((*(theDoc->vScrollBar))->contrlVis)
  233.             HideControl(theDoc->vScrollBar);
  234.         if ((*(theDoc->vScrollBar))->contrlVis)
  235.             HideControl(theDoc->hScrollBar);
  236.     }
  237. }        /* AdjustScrollbars */
  238.  
  239. #pragma segment Window
  240.  
  241. pascal void GetWinContentRect(WindowPtr theWindow, Rect *r)
  242. {
  243.     *r         = theWindow->portRect;
  244.     r->right  -= kScrollbarAdjust;
  245.     r->bottom -= kScrollbarAdjust;
  246. }  /* GetWinContentRect */
  247.  
  248. #pragma segment Window
  249.  
  250. pascal void InvalidateDocument(DPtr theDoc)
  251. {
  252.     GrafPtr oldPort;
  253.  
  254.     GetPort(&oldPort);
  255.     SetPort(theDoc->theWindow);
  256.     InvalRect(&theDoc->theWindow->portRect);
  257.     SetPort(oldPort);
  258. }
  259.  
  260. /* Called when the window has been resized to fix up the controls and content */
  261.  
  262. pascal void ResizeWindow(DPtr theDoc)
  263. {
  264.     AdjustScrollbars(theDoc, true);
  265.     AdjustTE(theDoc);
  266.     InvalidateDocument(theDoc);
  267. }         /* ResizeWindow */
  268.  
  269. /* Called when the window has been resized to fix up the controls and content */
  270.  
  271. pascal void ResizePageSetupForDocument(DPtr theDoc)
  272. {
  273.     theDoc->pageSize = (*(theDoc->thePrintSetup))->prInfo.rPage;
  274.  
  275.     OffsetRect(&(theDoc->pageSize), -theDoc->pageSize.left, -theDoc->pageSize.top);
  276.  
  277.     (*(theDoc->theText))->destRect.right = (*(theDoc->theText))->destRect.left +
  278.                                                         theDoc->pageSize.right;
  279.  
  280.     TECalText(theDoc->theText);
  281.  
  282.     ResizeWindow(theDoc);
  283. }         /* ResizePageSetupForDocument */
  284.  
  285. #pragma segment Main
  286.  
  287. /* Common algorithm for setting the new value of a control. It returns the actual amount
  288. the value of the control changed. Note the pinning is done for the sake of returning
  289. the amount the control value changed. */
  290.  
  291. pascal void CommonAction(ControlHandle control, short *amount)
  292. {
  293.     short   value;
  294.     short   max;
  295.  
  296.     value   = GetCtlValue(control); /* get current value */
  297.     max     = GetCtlMax(control); /* and max value */
  298.     *amount = value - *amount;
  299.     if (*amount < 0)
  300.           *amount = 0;
  301.     else if (*amount > max)
  302.         *amount = max;
  303.  
  304.     SetCtlValue(control, *amount);
  305.     *amount = value - *amount; /* calculate true change */
  306. }         /* CommonAction */
  307.  
  308. #pragma segment Main
  309.  
  310. /* Determines how much to change the value of the vertical scrollbar by and how
  311.   much to scroll the TE record. */
  312.  
  313. pascal void VActionProc(ControlHandle control, short part)
  314. {
  315.     short           amount;
  316.     WindowPtr       window;
  317.     DPtr            theDoc;
  318.  
  319.      if (part) {
  320.           window = (*control)->contrlOwner;
  321.         theDoc = DPtrFromWindowPtr(window);
  322.         switch (part) {
  323.         case inUpButton:
  324.         case inDownButton:
  325.             amount = 24;
  326.             break;
  327.  
  328.         case inPageUp:
  329.         case inPageDown:
  330.             amount = (*(theDoc->theText))->viewRect.bottom -
  331.                         (*(theDoc->theText))->viewRect.top;
  332.             break;
  333.         }   /* case */
  334.  
  335.         if (part == inDownButton || part == inPageDown)
  336.              amount = -amount; /* reverse direction */
  337.  
  338.         CommonAction(control, &amount);
  339.  
  340.         if (amount) {
  341.             TEScroll(0, amount, theDoc->theText);
  342.             DrawPageExtras(theDoc);
  343.         }
  344.     }     /* if */
  345. }  /* VActionProc */
  346.  
  347. #pragma segment Main
  348.  
  349. /* Determines how much to change the value of the horizontal scrollbar by and how
  350.   much to scroll the TE record. */
  351.  
  352. pascal void HActionProc(ControlHandle control, short part)
  353. {
  354.     short      amount;
  355.     WindowPtr  window;
  356.     DPtr       theDoc;
  357.  
  358.     if (part) {
  359.         window = (*control)->contrlOwner;
  360.         theDoc = DPtrFromWindowPtr(window);
  361.         switch (part) {
  362.         case  inUpButton:
  363.         case  inDownButton:
  364.             amount = kButtonScroll; /* a few pixels */
  365.             break;
  366.         case  inPageUp:
  367.         case  inPageDown:
  368.             amount = (*(theDoc->theText))->viewRect.right -
  369.                         (*(theDoc->theText))->viewRect.left; /* a page */
  370.             break;
  371.         }   /* switch */
  372.         if (part == inDownButton || part == inPageDown)
  373.             amount = - amount; /* reverse direction */
  374.  
  375.         CommonAction(control, &amount);
  376.         if (amount) {
  377.             TEScroll(amount, 0, theDoc->theText);
  378.             DrawPageExtras(theDoc);
  379.         }
  380.     }     /* if */
  381. }         /* HActionProc */
  382.  
  383. /**-----------------------------------------------------------------------
  384.         Name:         ShowSelect
  385.         Purpose:        Scrolls the text selection into view.
  386.     -----------------------------------------------------------------------**/
  387.  
  388. #pragma segment Window
  389.  
  390. pascal void ShowSelect(DPtr theDoc)
  391. {
  392.     if (!theDoc)
  393.         return;
  394.         
  395.      AdjustScrollbars(theDoc, false);
  396.  
  397.     /*
  398.         Let TextEdit do the hard work of keeping the selection visible…
  399.     */
  400.  
  401.     TEAutoView(true, theDoc->theText);
  402.     TESelView(theDoc->theText);
  403.     TEAutoView(false, theDoc->theText);
  404.  
  405.     /*
  406.         Now rematch the text and the scrollbars…
  407.     */
  408.  
  409.     SetCtlValue(
  410.         theDoc->hScrollBar,
  411.         (*(theDoc->theText))->viewRect.left -
  412.         (*(theDoc->theText))->destRect.left + kTextOffset);
  413.  
  414.     SetCtlValue(
  415.         theDoc->vScrollBar,
  416.         (*(theDoc->theText))->viewRect.top -
  417.         (*(theDoc->theText))->destRect.top  + kTextOffset);
  418. }  /* ShowSelect */
  419.  
  420. #pragma segment Window
  421.  
  422. pascal void OffsetWindow(WindowPtr aWindow)
  423. {
  424.     short theWidth;
  425.     short theHeight;
  426.     short theHScreen;
  427.     short theVScreen;
  428.     short xWidth;
  429.     short xHeight;
  430.     short hMax;
  431.     short vMax;
  432.     short wLeft;
  433.     short wTop;
  434.  
  435.     theWidth  = aWindow->portRect.right - aWindow->portRect.left;
  436.     theHeight = aWindow->portRect.bottom - aWindow->portRect.top + kTBarHeight;
  437.  
  438.     theHScreen = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  439.     theVScreen = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  440.  
  441.     xWidth  = theHScreen - theWidth;
  442.     xHeight = theVScreen - (theHeight + kMBarHeight);
  443.  
  444.     hMax = (xWidth / kVOffset) + 1;
  445.     vMax = (xHeight / kVOffset) + 1;
  446.  
  447.     gWCount++;
  448.  
  449.     wLeft = (gWCount % hMax) * kVOffset;
  450.     wTop  = ((gWCount % vMax) * kVOffset) + kTBarHeight + kMBarHeight;
  451.  
  452.     MoveWindow(aWindow, wLeft, wTop, false);
  453. }
  454.  
  455.  
  456. /* Returns the update region in local coordinates */
  457.  
  458. pascal void GetLocalUpdateRgn(WindowPtr window, RgnHandle localRgn)
  459. {
  460.     CopyRgn(((WindowPeek)window)->updateRgn, localRgn); /* save old update region */
  461.     OffsetRgn(localRgn, window->portBits.bounds.left, window->portBits.bounds.top); /* convert to local coords */
  462. }          /* GetLocalUpdateRgn */
  463.  
  464. #pragma segment Window
  465.  
  466. pascal void IssueZoomCommand(WindowPtr whichWindow, short whichPart);
  467. pascal void IssueSizeWindow(WindowPtr whichWindow,short newHSize, sWidth;
  468.  
  469.     vScrollRect.bottom = vScrollRect.bottom - 14;
  470.     vScrollRect.top    = vScrollRect.top - 1;
  471.  
  472.     vScroll = NewControl(myWindow, &vScrollRect, "", true, 0, 0, 0, scrollBarProc, 0);
  473.  
  474.     hScrollRect = myWindow->portRect;
  475.     hScrollRect.top = hScrollRect.bottom - kScrollbarAdjust;
  476.     hScrollRect.bottom = hScrollRect.top + kScrollbarWidth;
  477.  
  478.     hScrollRect.right = hScrollRect.right - 14;
  479.     hScrollRect.left  = hScrollRect.left + 31;
  480.     hScroll = NewControl(myWindow, &hScrollRect, "", true, 0, 0, 0, scrollBarProc, 0);
  481.  
  482.     myDoc->vScrollBar = vScroll;
  483.     myDoc->hScrollBar = hScroll;
  484.     myDoc->type            = kPlainTextDoc;
  485.     myDoc->kind         = kind;
  486.     myDoc->dirty         = false;
  487.  
  488.     if (kind == kDocumentWindow) {
  489.         myDoc->u.reg.lastID            = 0;
  490.         myDoc->u.reg.firstSection    = nil;
  491.         myDoc->u.reg.lastSection    = nil;
  492.         myDoc->u.reg.numSections    = 0;
  493.         myDoc->u.reg.everSaved     = false;
  494.         myDoc->u.reg.showBorders     = false;
  495.         
  496.         myDoc->lastState                = stateDocument + stateRdWr;
  497.     } else {
  498.         myDoc->u.cons.next            = gConsoleList;
  499.         myDoc->u.cons.cookie            = nil;
  500.         myDoc->u.cons.fence            = 0;
  501.         myDoc->u.cons.memory            = 20000;
  502.         myDoc->u.cons.selected        = false;
  503.  
  504.         gConsoleList = myDoc;
  505.  
  506.         myDoc->lastState                = stateConsole + stateBlocked;
  507.     }
  508.  
  509.     GetTERect(myWindow, &viewRect);
  510.     destRect = viewRect;
  511.  
  512.     myDoc->thePrintSetup = (THPrint)NewHandle(sizeof(TPrint));
  513.  
  514.     resFile = CurResFile();
  515.     
  516.     PrOpen();
  517.     PrintDefault(myDoc->thePrintSetup);
  518.     PrClose();
  519.     
  520.     UseResFile(resFile);
  521.  
  522.     myDoc->pageSize = (*(myDoc->thePrintSetup))->prInfo.rPage;
  523.     OffsetRect(&myDoc->pageSize, -myDoc->pageSize.left, -myDoc->pageSize.top);
  524.  
  525.     destRect.right = destRect.left + myDoc->pageSize.right;
  526.  
  527.     OffsetRect(&destRect, kTextOffset, kTextOffset);
  528.  
  529.     TextFont(gFormat.font);
  530.     TextSize(gFormat.size);
  531.     TextFace(0);
  532.  
  533.     myDoc->theText = TENew(&destRect, &viewRect);
  534.     
  535.     (*myDoc->theText)->crOnly = -1;
  536.     myDoc->theFileName[0] = 0;
  537.     myDoc->theWindow      = myWindow;
  538.  
  539. #ifndef RUNTIME
  540.     myDoc->tsmDoc                =    nil;
  541.     myDoc->tsmTERecHandle    =    nil;
  542.     
  543.     if (gTSMTEImplemented) {
  544.         supportedInterfaces[0] = kTSMTEInterfaceType;
  545.         if (NewTSMDocument(1, supportedInterfaces, &myDoc->tsmDoc,
  546.                     (long) &myDoc->tsmTERecHandle) == noErr)
  547.         {
  548.             TSMTERecPtr tsmteRecPtr = *(myDoc->tsmTERecHandle);
  549.             
  550.             tsmteRecPtr->textH = myDoc->theText;
  551.             tsmteRecPtr->preUpdateProc = nil;
  552.             tsmteRecPtr->postUpdateProc = nil;
  553.             tsmteRecPtr->updateFlag = 0;
  554.             tsmteRecPtr->refCon = (long) myDoc->theWindow;
  555.             
  556.             UseInputWindow(myDoc->tsmDoc, !gPerlPrefs.inlineInput);
  557.         } else {
  558.             myDoc->tsmDoc                =    nil;
  559.             myDoc->tsmTERecHandle    =    nil;
  560.         }
  561.     }
  562. #endif
  563.         
  564.     ResizeWindow(myDoc);
  565.     
  566.     RegisterDocument(myDoc);
  567.     
  568.     return(myDoc);
  569. }
  570.  
  571. #pragma segment Window
  572.  
  573. pascal void CloseMyWindow(WindowPtr aWindow)
  574. {
  575.     DPtr     aDocument;
  576.     TEHandle theText;
  577.  
  578.     DoHideWindow(aWindow);
  579.     aDocument = DPtrFromWindowPtr(aWindow);
  580.  
  581.     UnregisterDocument(aDocument);
  582.  
  583. #ifndef RUNTIME
  584.     if (aDocument->tsmDoc) {
  585.         FixTSMDocument(aDocument->tsmDoc);
  586.         // DeleteTSMDocument might cause crash if we don't deactivate first, so...
  587.         DeactivateTSMDocument(aDocument->tsmDoc);
  588.         DeleteTSMDocument(aDocument->tsmDoc);
  589.     }
  590. #endif
  591.  
  592.     if (aDocument->kind != kDocumentWindow) {
  593.         CloseConsole(aDocument->u.cons.cookie);
  594.         
  595.         if (gConsoleList == aDocument)
  596.             gConsoleList = aDocument->u.cons.next;
  597.         else {
  598.             DPtr doc = gConsoleList;
  599.             while (doc->u.cons.next != aDocument)
  600.                 doc = doc->u.cons.next;
  601.             doc->u.cons.next = aDocument->u.cons.next;
  602.         }
  603.     }
  604.     
  605.     theText   = aDocument->theText;
  606.     TEDispose(theText);
  607.  
  608.     if (aDocument->thePrintSetup)
  609.         DisposHandle((Handle)aDocument->thePrintSetup);
  610.  
  611.     DisposPtr((Ptr)aDocument);
  612.     DisposeWindow(aWindow);
  613.  
  614.     gWCount--;
  615. }
  616.  
  617. /*
  618.     Name     : PrintWindow
  619.     Function : Prints the document supplied in theDoc. askUser controls interaction
  620.                   with the user.
  621.  
  622.                          Uses extra memory equal to the size of the textedit use in the
  623.                          printed document.
  624. */
  625.  
  626. pascal void PrintWindow(DPtr theDoc, Boolean askUser)
  627. {
  628.     GrafPtr          oldPort;
  629.     TEHandle         printerTE;
  630.     TPPrPort         printerPort;
  631.     Rect                 printView;
  632.     PageEndsArray    pageBounds;
  633.     short              nPages;
  634.     short               pageCtr;
  635.     Boolean             abort;
  636.     short                resFile;
  637.     Rect                 rectToClip;
  638.     TPrStatus         thePrinterStatus;
  639.     DialogPtr         progressDialog;
  640.  
  641.     abort = false;
  642.  
  643.     /*
  644.         Preserve the current port
  645.     */
  646.     GetPort(&oldPort);
  647.     resFile = CurResFile();
  648.     PrOpen();
  649.  
  650.     if (askUser)
  651.         if (abort = !PrJobDialog(theDoc->thePrintSetup)) {
  652.             PrClose();
  653.             
  654.             goto done;
  655.         }
  656.  
  657.     progressDialog = GetNewDialog(1005, nil, (WindowPtr)-1);
  658.  
  659.     DrawDialog(progressDialog);
  660.  
  661.     printerPort = PrOpenDoc(theDoc->thePrintSetup, nil, nil);
  662.     SetPort((GrafPtr)printerPort);
  663.  
  664.     /*
  665.         Put the window text into the printer port
  666.     */
  667.     TextFont((*theDoc->theText)->txFont);
  668.     TextSize((*theDoc->theText)->txSize);
  669.  
  670.     printView = (*(theDoc->thePrintSetup))->prInfo.rPage;
  671.     printerTE = TENew(&printView, &printView);
  672.  
  673.     HLock((Handle)((*(theDoc->theText))->hText));
  674.  
  675.     TESetText(*((*(theDoc->theText))->hText), (*(theDoc->theText))->teLength, printerTE);
  676.  
  677.     HUnlock((Handle)((*(theDoc->theText))->hText));
  678.  
  679.     /*
  680.         Work out the offsets
  681.     */
  682.     (*printerTE)->destRect = printView; /* GetPageEnds calls TECalText */
  683.  
  684.     GetPageEnds(printView.bottom-printView.top, printerTE, pageBounds, &nPages);
  685.  
  686.     TEDeactivate(printerTE);
  687.  
  688.     for (pageCtr = 0; pageCtr <= nPages-1; pageCtr++)
  689.         if (!abort) {
  690.             PrOpenPage(printerPort, nil);
  691.  
  692.             rectToClip = printView;
  693.  
  694.             if (pageCtr > 0)
  695.                 rectToClip.bottom = rectToClip.top + (pageBounds[pageCtr]-pageBounds[pageCtr-1]);
  696.             else
  697.                 rectToClip.bottom = rectToClip.top + pageBounds[pageCtr];
  698.  
  699.             ClipRect(&rectToClip);
  700.  
  701.             if (PrError() == iPrAbort)
  702.                 abort = true;
  703.  
  704.             if (! abort)
  705.                 TEUpdate(&printView, printerTE);
  706.  
  707.             if (PrError() == iPrAbort)
  708.                 abort = true;
  709.  
  710.             PrClosePage(printerPort);
  711.  
  712.             TEScroll(0,rectToClip.top-rectToClip.bottom, printerTE);
  713.         }
  714.  
  715.     TEDispose(printerTE);
  716.     PrCloseDoc(printerPort);
  717.  
  718.     if (( (*(theDoc->thePrintSetup))->prJob.bJDocLoop == bSpoolLoop ) &&
  719.             ( PrError() == noErr )  &&
  720.             (! abort))
  721.         PrPicFile( theDoc->thePrintSetup, nil, nil, nil, &thePrinterStatus);
  722.  
  723.     PrClose();
  724.  
  725.     DisposDialog(progressDialog);
  726.  
  727. done:
  728.     SetPort(oldPort);
  729.     UseResFile(resFile);
  730.     InvalRect(&oldPort->portRect);
  731. }
  732.  
  733. void ForceStatusRedraw(WindowPtr win)
  734. {
  735.     GrafPtr    oldPort;
  736.     Rect        r;
  737.     
  738.     GetPort(&oldPort);
  739.     SetPort(win);
  740.  
  741.     r = win->portRect;
  742.     
  743.     r.right = 30;
  744.     r.top   = r.bottom - 13;
  745.     
  746.     InvalRect(&r);
  747.     
  748.     SetPort(oldPort);
  749. }
  750.  
  751. pascal void ShowWindowStatus()
  752. {
  753.      DPtr           aDocument;
  754.     WindowPeek    aWindow;
  755.     WindowPeek    nextWindow;
  756.     short            curState;
  757.  
  758.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  759.         nextWindow = aWindow->nextWindow;
  760.         if (Ours((WindowPtr) aWindow)) {
  761.             aDocument = DPtrFromWindowPtr((WindowPtr) aWindow);
  762.  
  763.             if (aDocument->kind == kDocumentWindow) 
  764.                 curState = stateDocument + stateRdWr;
  765.             else {
  766.                 curState = stateConsole;
  767.                 if (aDocument->u.cons.fence == 32767)
  768.                     curState    += stateRdOnly;
  769.                 else if (!gRunningPerl || !aDocument->u.cons.selected)
  770.                     curState += stateBlocked;
  771.                 else
  772.                     curState += stateRdWr;
  773.             }
  774.             
  775.             if (curState != aDocument->lastState) {
  776.                 aDocument->lastState = curState;
  777.                 ForceStatusRedraw((WindowPtr) aWindow);
  778.             }
  779.         }
  780.     }
  781. }
  782.  
  783. #ifndef RUNTIME
  784. pascal void UseInlineInput(Boolean inline)
  785. {
  786.      DPtr           aDocument;
  787.     WindowPeek    aWindow;
  788.     WindowPeek    nextWindow;
  789.  
  790.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  791.         nextWindow = aWindow->nextWindow;
  792.         if (Ours((WindowPtr) aWindow)) {
  793.             aDocument = DPtrFromWindowPtr((WindowPtr) aWindow);
  794.             if (aDocument->tsmDoc)
  795.                 UseInputWindow(aDocument->tsmDoc, !inline);
  796.         }
  797.     }
  798. }
  799. #endif
  800.  
  801. pascal void DoShowWindow(WindowPtr win)
  802. {
  803.      WindowPeek    aWindow;
  804.     WindowPeek    nextWindow;
  805.  
  806.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  807.         nextWindow = aWindow->nextWindow;
  808.         if (Ours((WindowPtr) aWindow)) {
  809.             goto done;
  810.         }
  811.     }
  812.     
  813.     SetLongMenus();
  814. done:
  815.     ShowWindow(win);
  816. }
  817.  
  818. pascal void DoHideWindow(WindowPtr win)
  819. {
  820.      WindowPeek    aWindow;
  821.     WindowPeek    nextWindow;
  822.  
  823.     HideWindow(win);
  824.     
  825.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  826.         nextWindow = aWindow->nextWindow;
  827.         if (Ours((WindowPtr) aWindow)) {
  828.             return;
  829.         }
  830.     }
  831.     
  832.     SetShortMenus();
  833. }